from selenium import webdriver

##提供键盘按键的支持，比如：RETURN, F1, ALT等
from selenium.webdriver.common.keys import Keys

##WebDriver 将等待， 直到页面完全加载完毕（其实是等到”onload” 方法执行完毕）
driver = webdriver.Firefox()
driver.get("http://www.python.org")

##确认标题是否包含“Python”一词。 (译注：assert 语句将会在之后的语句返回false后抛出异常
assert "Python" in driver.title

##包含 name 属性的input输入框
elem = driver.find_element_by_name("q")

##清除input输入框中的文本
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
driver.find_element_by_test()

##为了确保某些特定的结果被找到
assert "No results found." not in driver.page_source
driver.close()

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class PythonOrgSearch(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def tearDown(self):
        self.driver.close()


##入口函数
if __name__ == "__main__":
    unittest.main()
